#inpend=0
#errors off
curtoloc
print cr "Make Message Files for FAST, written by Peter Campbell. v1"
changed=0
var file_len,len,ms,current,msg
const ml=72

#include extinput.fi

proc abort
    {
    cursor 24,0:print bios
    if error=0 then
	{
	while changed
	    {
	    print bios "Save changed file? [Y] ";
	    wait_yesno:
	    wait for keypressed
	    k=ucase key
	    if k='N' then print bios "N":goto finish_abort
	    if (k<>13) and (k<>'Y') then goto wait_yesno
	    print bios "Y"
	    create #1,name+2
	    write #1,file_len from ms|0:if error then goto save_error
	    close #1
	    terminate ;No beep.

	    save_error:
	    print bios "Error saving file.":beep
	    }
	}
    finish_abort:
    beep:terminate
    }

ms=allocate 16384/16:if error then print "Out of memory, needs 22k.":abort

function find_msg(nn)
    {
    default=file_len
    m=0
    while m below file_len
	{
	z=ms[m]b:m++
	if z=0 then
	    {
	    num=ms[m]:m+=2
	    if num=nn then return m-3
	    if num>nn then default=m-3:return -1
	    }
	}
    return -1
    }

proc delete_message
    {
    msg_next=msg+3
    while (ms[msg_next]b<>0) and (msg_next below file_len) msg_next++
    moveb file_len-msg_next+1 from ms|msg_next to ms|msg
    file_len-=msg_next-msg
    changed=1
    }

proc edit_message(edx)
    {
    if edx then
	{
	msg_next=msg+3
	while (ms[msg_next]b<>0) and (msg_next below file_len) msg_next++
	fillb ml from message with ' '
	moveb msg_next-msg-3 from ms|msg+3 to message
	}
    locate 23,1
    len=ext_input(message_string)-1
    if len<>-1 then
	{
	if edx then delete_message
	if len then
	    {
	    moveb file_len-msg from ms|msg to ms|msg+len+3
	    ms[msg]b=0
	    ms[msg+1]=current
	    moveb len from message to ms|msg+3
	    file_len+=len+3
	    }
	changed=1
	}
    }

proc display(nn)
    {
    cls
    locate 0,0:colour 120
    print " MESSAGES ";
    colour 7
    print "  File: ";
    colour 15:m=name+2:while peekb m print chr peek m;:m++
    colour 7
    print "  (";file_len;" bytes)." cr

    if nn<0 then nn=0
    if nn then	;If zero then just list all.
	{
	nn=find_msg(nn)
	if nn=-1 then nn=default
	}

    while nn below file_len
	{
	z=ms[nn]b:nn++
	if z then print "????? ?";:goto next_msg
	num=ms[nn]:nn+=2
	colour 7:if num=current then colour 15
	print num;" ";
	while (ms[nn]b<>0) and (nn below file_len) print chr ms[nn];:nn++
	if nn=file_len then goto end_disp

	next_msg:
	print
	if locpos>=21*160 then goto end_disp
	}

    end_disp:
    locate 21,0:repeat 80 print chr 205;
    locate 24,0:repeat 80 print chr 205;
    locate 23,0:print "[";:locate 23,ml+1:print "]";
    colour 15
    locate 22,0
    print "Message number to modify/delete/view or add: (p=print) ";
    }

print cr "Edit/Create file: ";
loctocur
inputs name
print
if peekb (name+2)=0 then abort

open #1,name+2
if error then
    {
    create #1,name+2
    if error then print "Can't create file!":abort
    }

file_len=read #1,16384 to ms|0
close #1
if error then print "Can't read file!":abort
if file_len=16384 then print "File truncated, more than 16k.":beep

current=0

forever
    {
    start_disp:
    display(current-19)
    loctocur
    current=input
    if current<0 then
	{
	print bios " (valid range is 1-32767)":beep
	repeat 40000 {}
	goto start_disp
	}
    if not current then
	{
	if video[locpos]b='0' then current=0:goto start_disp
	if video[locpos]b='p' then
	    {
	    lprint " MESSAGES   File: ";
	    m=name+2:while peekb m lprint chr peek m;:m++
	    lprint "  (";file_len;" bytes)." cr lf

	    nn=0
	    while nn below file_len
		{
		z=ms[nn]b:nn++
		if z then lprint "????? ?";:goto lnext_msg
		lprint ms[nn];" ";:nn+=2
		while (ms[nn]b<>0) and (nn below file_len) lprint chr ms[nn];:nn++
		if nn=file_len then goto start_disp

		lnext_msg:
		lprint
		}
	    goto start_disp
	    }
	abort
	}

    display(current-19):print current;
    locate 23,1
    msg=find_msg(current)
    if msg=-1 then
	{
	fillb ml from message with ' '
	msg=default
	edit_message(0)
	}
    else edit_message(1)
    }

name:
string 30

message_string:
datab ml,0
message:
space ml+1
